<template>
  <div class="editBox">
    <el-form :inline="inline" label-width="200px" label-position="top" class="form">
      <el-form-item :label="item.name?item.name+':':''" :prop="item.key" v-for="item in formList" :key="item.key">
        <el-input v-if="item.type=='input'" v-model="item.value" :placeholder="item.placeholder"></el-input>
        <el-upload
          v-if="item.type=='file'"
          action="#"
          :on-change="handleChange"
          accept=".png,.jpg"
          class="upload"
          :auto-upload="false"
          :show-file-list="false"
        >          
          <img v-if="imageUrl" :src="imageUrl" class="pic">
          <div v-else>
            <el-icon class="icon"><Picture /></el-icon>
            <div class="el-upload__text">Upload Cover</div>
          </div>
        </el-upload>
      </el-form-item>
    </el-form>
    <div class="list" :style="{height:`calc(100% - 280px - ${(formList.length-1)*50}px)`}">
      <el-empty description="暂无数据" :image-size="100" v-if="list.length==0"></el-empty>
      <draggable :list="list" group="item" @change="onEnd" item-key="id" v-if="isDrag">
          <template #item="{element ,index}">
           <div class="item drag">
              <div class="key">{{ index+1}}</div>
              <div class="con">
                <div class="img">
                  <img :src="element.image" alt="" />
                  <img class="del" src="@/assets/delete.png" alt="" @click="removeItem(element.id)"/>
                </div>
                <div class="txt">
                  <div class="title">{{ element.title }}</div>
                  <div class="week-view">
                    <div class="stat-item">
                      <el-icon>
                        <Clock />
                      </el-icon>
                      <span>{{ element.timeAgo }}</span>
                    </div>
                    <div class="stat-item">
                      <el-icon>
                        <View />
                      </el-icon>
                      <span>{{ element.views }}</span>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </template>      
      </draggable>
      <template v-else>
        <div class="item" v-for="(element,index) in list" :key="element.id">
          <div class="key">{{ index+1}}</div>
          <div class="con">
            <div class="img">
              <img :src="element.image" alt="" />
              <img class="del" src="@/assets/delete.png" alt="" @click="removeItem(element.id)"/>
            </div>
            <div class="txt">
              <div class="title">{{ element.title }}</div>
              <div class="week-view">
                <div class="stat-item">
                  <el-icon>
                    <Clock />
                  </el-icon>
                  <span>{{ element.timeAgo }}</span>
                </div>
                <div class="stat-item">
                  <el-icon>
                    <View />
                  </el-icon>
                  <span>{{ element.views }}</span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </template>
      

       
    </div>
    <div class="checks">
      <span>{{ list.length }} have been selected</span>
    </div>
    <div class="btn" @click="handleAdd" :class="list.length==0?'dis':''">Create</div>
  </div>
</template>
<script setup>
import { ref ,reactive,defineProps,defineEmits } from "vue";
import {  Clock, View,Picture } from '@element-plus/icons-vue'
import draggable from 'vuedraggable';
const props = defineProps({
  list: {
    type: Array,
    default: []
  },
  inline:{
    type: Boolean,
    default: false
  },
  isDrag:{
    type: Boolean,
    default: false
  },
  formList:{
    type: Array,
    default: []
  }
});
const emit=defineEmits(['create','remove','sort'])
const onEnd = () => {
  emit('sort',props.list)
};
const imageUrl=ref('')

// 新增
const handleChange= (file,fileList)=> {
    props.formList.forEach(item=>{
      if(item.type=='file'){
        item.value=file
      }
    })
    imageUrl.value = URL.createObjectURL(file.raw);
  }
const handleAdd=()=>{
  let data={
    list:props.list,
    form:props.formList
  }
  emit('create',data)
}
// 移除
const removeItem=(id)=>{
   emit('remove',id)
}
</script>
<style lang='scss' scoped>
.drag{
  cursor: move;
}
.editBox {
  position: fixed;
  top: 80px;
  right: 0;
  bottom: 0;
  width: 422px;
  background: #302d3d;
  z-index: 999;
  padding: 16px 0;
  .pic{
    max-height: 100%;
    max-width: 100%;
  }
  .form {
    padding: 0 22px;
    :deep(.el-input) {
      height: 40px;
      background: #25202f;
      border-radius: 10px;
      border: 1px solid #5f4f55;
    }
    :deep(.el-input__wrapper) {
      border-radius: 10px;
      border: none;
      box-shadow: none;
    }
    .upload {
      width: 379px;
      height: 122px;
      background: #25202f;
      border-radius: 10px;
      border: 1px solid #5f4f55;
      text-align: center;
      color: RGBA(227, 66, 52, 1);
      font-size: 18px;
      display: flex;
      flex-direction: column;
      justify-content: center;
      :deep(.el-upload) {
        flex-direction: column;
        text-align: center;
      }
      .icon {
        font-size: 30px;
      }
    }
  }
  :deep(.el-upload){
    height: 100%;
  }
  .list {
    height: calc(100% - 330px);
    overflow: auto;
    .item {
      display: flex;
      margin-bottom: 10px;
      &:hover{
        .del{
            display: block;
        }
      }
      .key {
        width: 22px;
        text-align: center;
        font-family: Source Han Sans CN;
        font-weight: 400;
        font-size: 16px;
        color: #fbfbfb;
        line-height: 32px;
      }
      .con {
        width: 381px;
        height: 120px;
        display: flex;
        align-items: center;
        background: #413d53;
        border-radius: 10px;
        padding: 10px;
      }
      .del{
          display: none;
      }
      .img {
        width: 110px;
        flex-shrink: 0;
        height: 100%;
        position: relative;
        img {
          width: 100%;
          height: 100%;
          border-radius: 8px;
          &.del{
            cursor: pointer;
            position: absolute;
            left: 50%;
            top: 50%;
            margin: -12px 0 0 -12px;
            width: 24px;
            height: 24px;
          }
        }
      }
      .txt {
        flex: 1;
        margin-left: 10px;
        .title {
          font-family: Source Han Sans CN;
          font-weight: 500;
          font-size: 18px;
          color: #999999;
          line-height: 24px;
          margin-bottom: 25px;
        }

        .week-view {
          display: flex;
          justify-content: space-between;
        }

        .stat-item {
          display: flex;
          align-items: center;
          gap: 4px;
          font-size: 16px;
          color: rgba(153, 153, 153, 1);
          .el-icon {
            font-size: 17px;
          }
        }
      }
    }
  }
  .checks {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    padding: 0 22px;
    font-family: Source Han Sans CN;
    font-weight: 400;
    font-size: 16px;
    color: #fbfbfb;
    line-height: 32px;
    margin-top: 10px;
    :deep(.el-checkbox__inner) {
      border-color: #fff;
      border-radius: 50%;
    }
  }
  .btn {
    width: 381px;
    height: 40px;
    margin: 20px auto;
    background: #e34234;
    border-radius: 10px;
    border: 1px solid #db4235;
    font-family: Source Han Sans CN;
    font-weight: 400;
    font-size: 18px;
    color: #ffffff;
    line-height: 38px;
    text-align: center;
    cursor: pointer;
    &:hover {
      opacity: 0.7;
    }
    &.dis{
      cursor: not-allowed;
      opacity: 0.5;
    }
  }
}
:deep(.el-form--inline.el-form--label-top .el-form-item){
    display: inline-flex;
    width: 100%;
    margin-right: 0; 
    align-items: center;  
}
.el-form--inline.el-form--label-top{
     :deep(.el-form-item--label-top .el-form-item__label){
        width: 120px;
        margin-bottom: 0;
    }
}

</style>